<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable; 
use Tymon\JWTAuth\Contracts\JWTSubject;
use App\Models\Bill;

class User extends Authenticatable implements JWTSubject
{
    use HasFactory;
    protected $fillable = [
        'name',
        'avatar',
        'password',
        'mobile',
        'is_admin' 
    ];
    protected $casts = [
        'is_admin' => 'boolean'
    ];
    protected $hidden = [
        'password', // 隐藏密码字段
    ];
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }

    public function bills() {
        return $this->hasMany(Bill::class);
    }

    public function balance()
{
    return $this->hasOne(Balance::class);
}

public function budgets()
{
    return $this->hasMany(Budget::class);
}

// 在创建用户时自动创建余额记录
protected static function booted()
{
    static::created(function ($user) {
        $user->balance()->create(['balance' => 0]);
    });
}
   
}
